package cn.anecansaitin.whimsy.common.effect.ow.doomfist;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.client.registers.ModSounds;
import cn.anecansaitin.whimsy.common.attachment.DoomfistSkill;
import cn.anecansaitin.whimsy.common.register.ModDamageSources;
import cn.anecansaitin.whimsy.common.register.ModDataAttachments;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import cn.anecansaitin.whimsy.util.HitResultUtil;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent;

import java.util.Set;

@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class SeismicSlam extends MobEffect {
    public SeismicSlam() {
        super(MobEffectCategory.BENEFICIAL, 0);
    }

    @Override
    public void onEffectStarted(LivingEntity livingEntity, int amplifier) {
        livingEntity.setPos(livingEntity.position().add(0, 0.1, 0));
        livingEntity.setOnGround(false);
        livingEntity.level().playSound(null, livingEntity.getX(), livingEntity.getY(), livingEntity.getZ(), ModSounds.SEISMIC_SLAM_BEGIN.get(), SoundSource.PLAYERS);
    }

    @Override
    public void applyEffectTick(LivingEntity livingEntity, int amplifier) {
        if (!livingEntity.onGround() || livingEntity.level().isClientSide) {
            return;
        }

        livingEntity.removeEffect(this);
        livingEntity.level().playSound(null, livingEntity.getX(), livingEntity.getY(), livingEntity.getZ(), ModSounds.SEISMIC_SLAM_END.get(), SoundSource.PLAYERS);
    }

    @Override
    public boolean shouldApplyEffectTickThisTick(int furation, int amplifier) {
        return true;
    }

    @SubscribeEvent
    public static void onEffectRemove(MobEffectEvent.Remove event) {
        if (event.getEffect() == ModEffects.SEISMIC_SLAM.get()) {
            onEffectFinished(event.getEntity());
        }
    }

    @SubscribeEvent
    public static void onEffectExpired(MobEffectEvent.Expired event) {
        if (event.getEffectInstance().getEffect() == ModEffects.SEISMIC_SLAM.get()) {
            onEffectFinished(event.getEntity());
        }
    }

    private static void onEffectFinished(LivingEntity entity) {
        DoomfistSkill skill = entity.getMainHandItem().getData(ModDataAttachments.DOOMFIST_SKILL);
        skill.seismicSlam.castingComplete();
        Set<Entity> enemies = HitResultUtil.lowFlyingFanShapedRangeEntity(entity, 20, 100, 15);//todo 这个方法不好
        //获取临时护盾
        entity.setAbsorptionAmount(entity.getAbsorptionAmount() + enemies.size() * 3.5f);
        skill.ultimateEfficiency.generate();

        for (Entity enemy : enemies) {
            enemy.hurt(ModDamageSources.owNoKnockback(enemy.level(), entity, null, enemy.position()), 5);

            if (enemy instanceof LivingEntity living) {
                living.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 3 * 20, 1));
            }
        }
    }
}
